﻿using System.Net.Sockets;
using System.Threading;

namespace System.Net.DnsToolkit.SocketAsync {

    internal class UdpUserToken {

        internal Socket Socket { get; set; }
        internal bool SocketClosed { get; private set; }
        internal AutoResetEvent TimeoutEvent { get; private set; }
        internal EndPoint RemoteEndPoint { get; private set; }
        internal object Ticket { get; private set; }

        const bool InitialStateNonSignaled = false;

        internal UdpUserToken(EndPoint ep, Socket socket, object ticket) {

            RemoteEndPoint = ep;
            Socket = socket;
            Ticket = ticket;
            TimeoutEvent = new AutoResetEvent(InitialStateNonSignaled);

        }


        internal void CloseSocket() {

            SocketClosed = true;

            Socket.Shutdown(SocketShutdown.Both);
            if (Socket.Connected) {
                Socket.Disconnect(true);
            }

            Socket.Close(0);
            Socket = null;


        }
    }
}
